package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.SelfShop;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.SelfShopService;
import com.base.service.UsersService;
import com.base.utils.Page;

/**
 * 自营商城分类
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShopStore {
	@Autowired
	private SelfShopService selfShopService;
	@Autowired
	private UsersService usersService;

	// 自营商城分类首页
	@RequestMapping("/selfShopStore")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		Page page = new Page();
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<SelfShop> selfShops = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			page.setRecordTotal(selfShopService.countAll(adminUserId, null, null));
			selfShops = selfShopService.selectAll(page, adminUserId, null, null);
		}
		request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_SelfShopStore");
		request.setAttribute("selfShops", selfShops);
		return modelAndView;
	}
	
	@RequestMapping("/selectSelfShopStore")
	public ModelAndView selectSelfShopStore(HttpServletRequest request, Integer curPage, String shopname,
			Byte status) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        
		Integer choose = 2;
		if(shopname != null && !shopname.equals("") || status != 4) {
			choose = 1;
		}
		
		String fShopname = null;
		Byte fStatus = null;
		
		if(shopname != null && !shopname.equals("")) {
			fShopname = shopname;
		}
		if(status != 4) {
			fStatus = status;
		}
		
		page.setCurrentPageNo(curPage);
		List<SelfShop> selfShops = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			page.setRecordTotal(selfShopService.countAll(adminUserId, fShopname, fStatus));
			selfShops = selfShopService.selectAll(page, adminUserId, fShopname, fStatus);
		}
		
		if(choose == 2) {
			if(users.getRole() == 0 || users.getRole() == 1) {
				page.setRecordTotal(selfShopService.countAll(adminUserId, null, null));
				selfShops = selfShopService.selectAll(page, adminUserId, null, null);
			}
		}
		
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_SelfShopStore");
		request.setAttribute("selfShops", selfShops);
		request.setAttribute("status", status);
		request.setAttribute("shopname", shopname);
		return modelAndView;
	}
	
}
